$(function(){
	var address = getCookie("restAddress");
	var titleHtml = getTitleHtml(address);
	$("#restIntroduction").html(titleHtml);
	getDishHtml(address);
});

function getTitleHtml(address){
	var resName = getCookie("rName"+address);
	var result = "<center><h2>"+resName+"</h2></center><hr />";
	// result += "<div class = \"\"><button class=\"btn btn-primary pull-right\" id=\""+address+"\" onclick=\"selectDate(this)\">Book This</button></div>"
	result += "<div class = \"bookResults\"> <button class = \"btn btn-primary pull-right book-button\" value=\""+address+"\" id=\""+address+"\" onclick=\"selectDate(this)\">Book This</button></div>";
	result += "<div id=\"datePicker_"+address+"\" class=\"pull-right\" ></div>";
	result += "<div class = \"restDishes\"></div>";
	return result;
}

function getDishHtml(address){
	$.post("http://localhost/cs2102/src/getDish.php",
	{
		address:address
	},
	function(dataJson,status){
		var dat = $.parseJSON(dataJson);
		var i = 0;
		var dish = dat["dish0"];
		var dishHtml;

		while(dish != undefined){
			dishHtml = generateHtmlForDish(dish);
			$(".restDishes").append(dishHtml);
			i++;
			dish = dat["dish"+i];
		}
	});
}

function generateHtmlForDish(dish){
	var start = "<div class = \"row\">";
	var divStart = "<div class = \"col-md-9\">";
	var header = "<h3>"+dish.dishName+"</h3>";
	var dishBody = "<p>"+dish.info+"</p>";
	dishBody += "<br><p><strong>price: </strong>"+dish.price+"</p>";
	var divEnd = "</div>";
	var img = getImage(dish);
	return start+divStart+header+divEnd+divStart+dishBody+divEnd+img+divEnd;
}

function getImage(dish){
	var address = getCookie("restAddress");
	var resName = getCookie("rName"+address);

	var imgStart = "<div class = \"col-md-3\">";
	var imgPath = "../img/"+resName+"/"+dish.dishName+".jpg";
	var image = "<img src = \""+imgPath+"\" height = \"150\" width = \"150\" />";
	var divEndImg = "</div>";

	return imgStart + image + divEndImg;
}